<?php

namespace app\api\controller;

use app\admin\model\Hall;
use app\admin\model\OccupySeat;
use app\admin\model\Schedule as ScheduleModel;
use app\common\controller\Api;
use app\common\model\OrderTicket;

/**
 * 排期接口
 */
class Schedule extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['date', 'session', 'seatInfo'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /*
     * 日期排期范围
     * */
    public function date()
    {
        $ScheduleModel = new ScheduleModel;

        // 获取当前日期（格式：2025-04-21）
        $currentDate = date('Y-m-d');

        $date_range = [
            'min_date' => '',
            'max_date' => '',
        ];
        $list = $ScheduleModel->where(['status' => 1])->where('date', '>=', $currentDate)->field('date')->group('date')->select();
        if ($list) {
            $count = count($list);
            foreach ($list as $k => $v) {
                $date_range['min_date'] = $list[0]['date'];
                $date_range['max_date'] = $list[$count - 1]['date'];
            }
        }
        $this->success('日期排期', $date_range);
    }

    /**
     * 指定日期下场次
     */
    public function session()
    {
        $date = $this->request->get('date', '');
        $list = ScheduleModel::with(['hall'])->where(['date' => $date, 'status' => 1])->where('full_start_time', '>', datetime(time()))->order([
            'is_box' => 'asc',
            'full_start_time' => 'asc'
        ])->select();
        if ($list) {
            $OrderTicketModel = new OrderTicket();
            $OccupySeatModel = new OccupySeat();
            foreach ($list as $key => &$value) {
                $value['start_time'] = datetime($value['start_time'], 'H:i');
                $value['des_arr'] = explode(',', $value['des']);
                $hall = Hall::where(['id' => $value['hall_id']])->find();
                $hall_seat_data = json_decode($hall['seat_data'], true);
                $value['image'] = cdnurl($value['hall']['image'], true);
                if ($value['is_box']) {
                    continue;
                }
                //可选座位数量
                $seat_count = 0;
                $is_occupy = 0;
                foreach ($hall_seat_data as $key2 => $value2) {
                    foreach ($value2 as $k => $v) {
                        if ($v['status'] == 0) {
                            $seat_count++;
                            $order_seat = $OrderTicketModel->where(['hall_id' => $value['hall_id'], 'schedule_id' => $value['id']])
                                ->where(['x_coord' => $v['XCoord'], 'y_coord' => $v['YCoord']])
                                ->whereIn('status', [0, 1, 2])
                                ->find();
                            $occupy_seat = $OccupySeatModel->where(['schedule_id' => $value['id']])
                                ->where(['x_coord' => $v['XCoord'], 'y_coord' => $v['YCoord']])
                                ->find();
                            if ($order_seat || $occupy_seat) {
                                //有订单或者后台锁座-占座数+1
                                $is_occupy++;
                            }
                        }

                    }

                }
                //是否满座
                $value['is_full'] = $is_occupy >= $seat_count ? 1 : 0;
            }
        }
        // list按full_start_time进行排序


        $this->success('场次信息', $list);
    }


    /*
     * 选座页
     * */
    public function seatInfo()
    {
        $schedule_id = $this->request->get('schedule_id', 0);
        $info = ScheduleModel::with(['hall'])->where(['id' => $schedule_id, 'status' => 1])->find();
        if (!$info) {
            $this->error('场次不存在');
        }
        if ($info['operating_status'] == 0) {
            $this->error('该排期休息');
        }
        $info['image'] = cdnurl($info['hall']['image'],true);
        $info['warm_reminder'] = config('site.warm_reminder');
        if($info['is_box']){
            $info['box_image'] = cdnurl($info['box_image'],true);
            $info['box_reservation_phone'] = config('site.box_reservation_phone');
            $this->success('包厢详情页', $info);
        }
        $seat_normal_price = $info['seat_normal_price'];
        $seat_vip_price = $info['seat_vip_price'];

        $hall_seat = Hall::where(['id' => $info['hall_id']])->find();
        //        halt($hall_seat);
        //        $info['hall_name'] = $hall_seat['name'];
        $hall_seat_data = json_decode($hall_seat['seat_data'], true);
        $OrderTicketModel = new OrderTicket();
        $OccupySeatModel = new OccupySeat();
        foreach ($hall_seat_data as $key => &$value) {
            foreach ($value as $k => &$v) {
                if ($v['status'] == 0) {
                    //订单锁座
                    $order_seat = $OrderTicketModel->where(['hall_id' => $info['hall_id'], 'schedule_id' => $schedule_id])
                        ->where(['x_coord' => $v['XCoord'], 'y_coord' => $v['YCoord']])
                        //                        ->where('status','>=',0)
                        ->whereIn('status', [0, 1, 2])
                        ->find();
                    $occupy_seat = $OccupySeatModel->where(['schedule_id' => $schedule_id])
                        ->where(['x_coord' => $v['XCoord'], 'y_coord' => $v['YCoord']])
                        ->find();
                    if ($order_seat || $occupy_seat) $v['status'] = 2;
                }
                if ($v['level'] == 'normal') $v['Price'] = $seat_normal_price;
                if ($v['level'] == 'vip') $v['Price'] = $seat_vip_price;

            }
        }
        $info['hall_seat_data'] = $hall_seat_data;
        $this->success('选座页', $info);
    }
}
